checkLogin()
renderUsername()
registerLogout()


//渲染顶部数据的函数
function renderOverview(overview){
    Object.keys(overview).forEach(key =>{
        // console.log(key);
        document.querySelector(`.${key}`).innerHTML = overview[key]})
}

//渲染薪资走势
function renderYearSalary(year){
    // console.log(year);
    //初始化echarts对象
    const dom = document.querySelector('#line')
    const myChart = echarts.init(dom)
    //设置数据选项
    const option = {
        title:{
            text:'2022薪资走势',
            //距离容器左侧
            left:'12',
            //距离容器顶部
            top:'15'
        },
        xAxis: {
          type: 'category',
        //   data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        data: year.map(v=> v.month),
        axisLine:{
          lineStyle:{
            type:'dashed',
            //坐标轴颜色
            color:'#ccc'
          }
        }
        },
        yAxis: {
          type: 'value',
          //分割线为虚线
          splitLine:{
            lineStyle:{
                type:'dashed'
            }
        }
        },
        //绘图网格
        grid:{
            top:'20%'
        },
        series: [
          {
            // data: [820, 932, 901, 1000, 1290, 1330, 1320],
            data: year.map(v=> v.salary),
            //折线图
            type: 'line',
            symbolSize: 10,
            lineStyle:{
              width:8,
              color:{
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                    offset: 0, color: '#479dee' // 0% 处的颜色
                }, {
                    offset: 1, color: '#5c75f0' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            },
            //是否平滑
            smooth: true,
            //下方填充颜色
            areaStyle: {
              color:{
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#b2d7f7' // 0% 处的颜色
                }, {
                    offset: 1, color: 'rgba(255, 255, 255,0)' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            }
          }
        ],
        //提示框
        tooltip:{
          trigger:'axis'
        }
      };
    //
    myChart.setOption(option)
}
//渲染薪资分布
function renderSalary(salaryData){
  // console.log(salaryData);
  // 初始化对象
  const dom = document.querySelector('#salary')
  const myChart = new echarts.init(dom)
  // 定义属性和数据
  const option = {
    title:{
      text:'班级薪资分布',
      left:10,
      top:15
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      // top: '5%',
      bottom:'5%',
      left: 'center'
    },
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['55%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 15,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        // emphasis: {
        //   label: {
        //     show: true,
        //     fontSize: 40,
        //     fontWeight: 'bold'
        //   }
        // },
        label:{
          show:false
        },
        labelLine: {
          show: false
        },
        //是否启用防止标签重叠策略，默认开启
        // avoidLabelOverlap:false
        data: salaryData.map(v=> {
          return {
            value:v.g_count + v.b_count,
            name:v.label
          }
        })
        // [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        //   { value: 300, name: 'Video Ads' }
        // ]
      }
    ],
    color:['#fda224','#5097ff','#3abcfa','#34d39a']
  };
  // 渲染图表
  myChart.setOption(option)
}
//渲染每组薪资
function renderGroupSalary(groupData){
  //
  const dom = document.querySelector('#lines')
  const myChart = new echarts.init(dom)

  const option = {
    xAxis: {
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed'
        }
      },
      axisLabel:{
        color:'#999'
      },
      type: 'category',
      // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      data: groupData[1].map(v=> v.name)
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
    },
    tooltip: {},
    grid:{
      left:70,
      top:30,
      right:30,
      bottom:50
    },
    series: [
      {
        name:'期望薪资',
        // data: [120, 200, 150, 80, 70, 110, 130],
        data:groupData[1].map(v=> v.salary),
        type: 'bar',
        itemStyle:{
          color:{
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
                offset: 0, color: '#34D39A' // 0% 处的颜色
            }, {
                offset: 1, color: 'rgba(52, 211, 154, 0.2)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      },
      {
        name:'实际薪资',
        data:groupData[1].map(v=> v.hope_salary),
        // data: [120, 200, 150, 80, 70, 110, 130],
        type: 'bar',
        itemStyle:{
          color:{
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
                offset: 0, color: '#499fEE' // 0% 处的颜色
            }, {
                offset: 1, color: 'rgba(73, 159, 238, 0.2)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
        
      }
    ]
  };
myChart.setOption(option)  
//高亮切换
const btns = document.querySelector('#btns')
btns.addEventListener('click',(e)=>{
  if(e.target.classList.contains('btn')){
    // console.log(11);
    document.querySelector('.btn-blue').classList.remove('btn-blue')
    e.target.classList.add('btn-blue')
    const index = e.target.innerText
    const data = groupData[index]
    option.xAxis.data = data.map(v => v.name)
    option.series[0].data = data.map(v => v.hope_salary)
    option.series[1].data = data.map(v => v.salary)
    myChart.setOption(option)

  }
})
}

//渲染男女薪资分布函数
function renderGenderSalary(salaryData){
  // console.log(salaryData);
  //老三步
  const dom = document.querySelector('#gender')
  const myChart = new echarts.init(dom)
  const option = {
    tooltip: {
      trigger: 'item'
    },
    // legend: {
    //   top: '5%',
    //   left: 'center'
    // },
    title: [
      {
        text: '男女薪资分布',
        left: 10,
        top:10,
        textStyle:{
          fontSize:16
        }
      },
      {
        text: '男生',
        left: '50%',
        top: '45%',
        textStyle:{
          fontSize:12
        }
        
      },
      {
        text: '女生',
        left: '50%',
        top: '85%',
        textStyle:{
          fontSize:12
        }
      }
    ],
    color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
    series: [
      {
        type: 'pie',
        radius: ['20%', '30%'],
        center: ['50%', '30%'],
        // data: [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        //   { value: 300, name: 'Video Ads' }
        // ]
        data:salaryData.map(v=> {
          return {value:v.b_count, name:v.label}
        })
      },
      {
        type: 'pie',
        radius: ['20%', '30%'],
        center: ['50%', '70%'],
        // data: [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        //   { value: 300, name: 'Video Ads' }
        // ]
        data:salaryData.map(v=> {
          return {value:v.g_count, name:v.label}
        })
      }
    ]
  };
  myChart.setOption(option)  
}

//渲染籍贯分布
function renderProvince(provinceData){
  // console.log(provinceData);
  const dom = document.querySelector('#map')
  const myEchart = echarts.init(dom)
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]
  //筛选数据
  dataList.forEach(item => {
    const res = provinceData.find(v=> {
      return v.name.includes(item.name)
    })
    // console.log(res);
    if(res){
      item.value = res.value
    }
  })
  const option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList,
      },
    ],
  }
  myEchart.setOption(option)
}


//首页统计数据
//因为很多逻辑需要用到此接口返回的逻辑，把渲染的部分进行抽取
async function getData(){
    //拦截器写法（只保留核心逻辑即可）
    const res = await axios({
        url:'/dashboard',
    })
    // const overview = res.data.overview
    // console.log(res);
    const {overview ,year,salaryData, groupData,provinceData} = res.data
    renderOverview(overview)
    // Object.keys(overview).forEach(key =>{
    //     // console.log(key);
    //     document.querySelector(`.${key}`).innerHTML = overview[key]})
    renderYearSalary(year)
    renderSalary(salaryData)
    renderGroupSalary(groupData)
    //调用男女薪资分布表
    renderGenderSalary(salaryData)
    //渲染籍贯分布
    renderProvince(provinceData)
    //没有使用拦截器写法-------------------------    
    //获取token
    // const token = localStorage.getItem('token')
    // try{
    // //调用接口(登录成功以后才能调用接口)
    // const res = await axios({
    //     url:'/dashboard',
    //     // headers:{
    //     //     Authorization:token
    //     // }
    // })
    // // console.log(res);
    // const overview = res.data.data.overview
    // // console.log(overview);
    // Object.keys(overview).forEach(key =>{
    //     // console.log(key);
    //     document.querySelector(`.${key}`).innerHTML = overview[key]
    // })
    // }catch(error){
    //     token无效的情况
    //     console.dir(error);
    //     if(error.response.status === 401){
    //         //清除缓存
    //         localStorage.removeItem('username')
    //         localStorage.removeItem('token')

    //         showToast('请重新登录')
    //         //返回首页
    //         setTimeout(() => {
    //             location.href = 'login.html'    
    //         }, 1500);
    //     }
    }

getData()